﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace _2D
{
    public class AnimationXML
    {
        private Vector2 position;
        private Texture2D spriteSheet;
        private XmlDocument animationXML;
        private int frame;
        private int framecount;
        private float timePerFrame;
        private float totalElapsed;


        private struct frameStruct
        {
            public int frameX;
            public int frameY;
            public int frameWidth;
            public int frameHeight;
        }

        private frameStruct[] frames;

        public AnimationXML(Vector2 pos, int frameCount, int framesPerSec, int bbwidth, int bbheight)
        {
            position = pos;
            frame = 0;
            framecount = frameCount;
            timePerFrame = 1.0f / framesPerSec;
            frames = new frameStruct[framecount];
            
        }

        public void Load(ContentManager content, string textureName, string XMLFile)
        {
            spriteSheet = content.Load<Texture2D>(textureName);
            animationXML = new XmlDocument();
            animationXML.Load(XMLFile);

            XmlNodeList sprites = animationXML.GetElementsByTagName("Sprites");

            XmlNodeList sprite =
            ((XmlElement)sprites[0]).GetElementsByTagName("Sprite");

            int i = 0;
            frameStruct frameAux;
            foreach (XmlElement nodo in sprite)
            {
                frameAux.frameX = int.Parse(nodo.GetElementsByTagName("X")[0].InnerText);
                frameAux.frameY = int.Parse(nodo.GetElementsByTagName("Y")[0].InnerText);
                frameAux.frameHeight = int.Parse(nodo.GetElementsByTagName("Height")[0].InnerText);
                frameAux.frameWidth = int.Parse(nodo.GetElementsByTagName("Width")[0].InnerText);

                frames[i] = frameAux;
                i++;
            }
        }

        public void Load(ContentManager content, string textureName, XmlNodeList animationElement)
        {
            spriteSheet = content.Load<Texture2D>(textureName);
            
            int i = 0;
            frameStruct frameAux;
            foreach (XmlElement nodo in animationElement)
            {
                frameAux.frameX = int.Parse(nodo.GetElementsByTagName("X")[0].InnerText);
                frameAux.frameY = int.Parse(nodo.GetElementsByTagName("Y")[0].InnerText);
                frameAux.frameHeight = int.Parse(nodo.GetElementsByTagName("Height")[0].InnerText);
                frameAux.frameWidth = int.Parse(nodo.GetElementsByTagName("Width")[0].InnerText);

                frames[i] = frameAux;
                i++;
            }
        }

        public void Draw(SpriteBatch batch)
        {
            int FrameWidth = spriteSheet.Width / framecount;

            Rectangle sourcerect = new Rectangle(frames[frame].frameX, frames[frame].frameY,
                frames[frame].frameWidth, frames[frame].frameHeight);

            batch.Draw(spriteSheet, position, sourcerect, Color.White,
                0, new Vector2(0, 0), 1, SpriteEffects.None, 1);
        }

        // TODO: Revisar parámetro scale.
        public void Draw(SpriteBatch batch, Vector2 pos, float scale, Vector2 cameraPosition)
        {
            int FrameWidth = spriteSheet.Width / framecount;

            Rectangle sourcerect = new Rectangle(frames[frame].frameX, frames[frame].frameY,
                frames[frame].frameWidth, frames[frame].frameHeight);

            batch.Draw(spriteSheet, pos, sourcerect, Color.White,
                0, new Vector2(cameraPosition.X, cameraPosition.Y), 1, SpriteEffects.None, 1);
        }

        public void Update(float elapsed)
        {
            totalElapsed += elapsed;
            if (totalElapsed > timePerFrame)
            {
                frame++;
                //Los frames son circulares, al llegar al último volvemos al primero.
                frame = frame % framecount;
                totalElapsed -= timePerFrame;
            }
        }

    }
}
